package com.google.gwt.indexeddb.client;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * The IDBRequest interface provides means to access results of asynchronous
 * requests to databases and database objects using event handler attributes See
 * http://www.w3.org/TR/IndexedDB/#request-api When a request is made, a new
 * request is returned with its readyState set to LOADING. If a request
 * completes successfully, the readyState is changed to DONE, the result is set
 * to the result of the request, and a event with type success is fired at the
 * request. If an error occurs while performing the operation, the readyState is
 * changed to DONE, the errorCode is set to the code of the error, and a event
 * with type error is fired at the request
 */
public class IDBRequest extends JavaScriptObject {

  protected IDBRequest() {
  }

  /**
   * This state indicates that a result to a previous request is available in
   * the result attribute.
   */
  public static final int DONE = 2;

  /**
   * This state indicates that a request has been started but its results is not
   * yet available.
   */
  public static final int LOADING = 1;

  /**
   * @param handler The IDBErrorEvent interface of the IndexedDB API represents
   *        an event related to an error condition in carrying out the request
   *        on the source.
   */
  public final native void setOnError(IDBErrorHandler handler) /*-{
		this.onerror = $entry(function(e) {
			handler.@com.google.gwt.indexeddb.client.IDBErrorHandler::onerror(Lcom/google/gwt/indexeddb/client/IDBErrorEvent;)(e);
		});
  }-*/;

  /**
   * @param handler The IDBErrorEvent interface of the IndexedDB API represents
   *        an event related to an error condition in carrying out the request
   *        on the source.
   */
  public final native void setOnSuccess(IDBSuccessHandler handler) /*-{
		this.onsuccess = $entry(function(e) {
			handler.@com.google.gwt.indexeddb.client.IDBSuccessHandler::onsuccess(Lcom/google/gwt/indexeddb/client/IDBSuccessEvent;)(e);
		});
  }-*/;

  /**
   * @return The state of the request. Every request starts in the LOADING
   *         state. The state changes to DONE when the request completes
   *         successfully, or when an error occurs. The readyState is set to
   *         DONE in two situations, with different outcomes: If the request
   *         completes successfully, then the result of the request is included
   *         as the result of a new IDBSuccessEvent object, which is queued to
   *         fire with the name success. If an error occurs, then the error code
   *         and message are included in a new IDBErrorEvent object, which is
   *         queued to fire with the name error. The event fired in these
   *         situations has no namespace, does not bubble, and is not cancelable
   *         at each window object.
   */
  public final native int getReadyState() /*-{
		return this.readyState;
  }-*/;

}
